<template>
  <div>
    <el-upload
      v-show="!isShowProcess"
      class="avatar-uploader"
      action="#"
      :http-request="onRequest"
      list-type="picture-card"
      :file-list="fileList"
      :on-preview="onPreview"
      :on-remove="onRemove"
      accept="image/jpg, image/png"
      :before-upload="beforeUpload"
      :on-change="onChange"
      :limit="limit"
      :on-exceed="onExceed"
    >
      <i class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
    <el-progress v-if="isShowProcess" type="circle" :percentage="percentage" />
    <!-- 头像弹层 -->
    <el-dialog
      title="头像"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <img :src="imgUrl">
    </el-dialog>

  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
var cos = new COS({
  SecretId: 'AKIDEH4apnVx94h4AWcRzGGzRcW8VaOvBAsO',
  SecretKey: 'GFxOYR3OSGn31qOghRv84i7F5exzkERO'
})

export default {
  props: {
    limit: {
      type: Number,
      default: () => 0
    }
  },
  data() {
    return {
      imgUrl: '',
      dialogVisible: false,
      fileList: [],
      percentage: 0,
      isShowProcess: false
    }
  },

  created() {

  },

  methods: {
    onRequest(data) {
      this.isShowProcess = true
      this.id = setTimeout(() => {
        if (this.percentage < 95) {
          this.percentage++
        } else {
          clearTimeout(this.id)
          this.percentage = 95
        }
      }, 800)
      cos.putObject({
        Bucket: 'hrsaas-1308960770', /* 必须 */
        Region: 'ap-shanghai', /* 存储桶所在地域，必须字段 */
        Key: data.file.name, /* 必须 */
        StorageClass: 'STANDARD',
        Body: data.file, // 上传文件对象
        onProgress: progressData => {
          if (progressData.percent === 1) {
            this.percentage = 100
            clearTimeout(this.id)
            setTimeout(() => {
              this.isShowProcess = false
              this.percentage = 0
            }, 500)
          }
        }
      }, (err, data) => {
        console.log(err || data)
        if (err) {
          this.isShowProgress = false
          this.$message.error('网络错误,上传失败')
          this.fileList.splice(this.fileList.length - 1, 1)
        }
        if (!err && data.statusCode === 200) {
          const lastUrl = this.fileList.length - 1
          this.fileList[lastUrl].url = 'https://' + data.Location
          this.$message.success('上传成功')
          this.$emit('on-success', this.fileList)
        }
      })
    },
    onPreview(file) {
      this.imgUrl = file.url
      this.dialogVisible = true
    },
    onRemove(file, fileList) {
      this.fileList = fileList
    },
    beforeUpload(file) {
      const imgType = ['image/jpg', 'image/png']
      if (imgType.includes(file.type)) {
        this.$message.error('请上传jpg和png格式的图片')
        return false
      }
      const maxSize = 1 * 1024 * 1024
      if (file.size > maxSize) {
        this.$message.error('请上传1MB以内的图片')
        return false
      }
    },
    onChange(file, fileList) {
      this.fileList = fileList
    },
    onExceed() {
      this.$message.error(`图片最多上传${this.limit}张`)
    }
  }
}
</script>

<style  lang='scss'>
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 148px;
    height: 148px;
    line-height: 148px;
    text-align: center;
  }
</style>
